package gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class Pairing {
	private WindowsApplication windowsApp;
	private JPanel pnlPairing;
	
	public Pairing(){
		initialize();
	}
	public Pairing(WindowsApplication windowsApp){
		this.windowsApp = windowsApp;
		initialize();
	}
	
	private void initialize(){
		pnlPairing = new JPanel();
		pnlPairing.setBounds(0, 0, 800, 600);
		pnlPairing.setLayout(null);
		
		final JLabel lblPairKey = new JLabel(getRSAExp());//get key exponent from config file
		lblPairKey.setHorizontalAlignment(SwingConstants.CENTER);
		lblPairKey.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 35));
		lblPairKey.setBounds(100, 100, 600, 100);
		pnlPairing.add(lblPairKey);
		
		JLabel lblPairingPage = new JLabel(windowsApp.getDevice().getDeviceName() + " Pairing Key");
		lblPairingPage.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 13));
		lblPairingPage.setBounds(100, 39, 600, 32);
		pnlPairing.add(lblPairingPage);
		
		JButton btnGeneratePairingKey = new JButton("Generate New Pairing Key");
		btnGeneratePairingKey.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 13));
		btnGeneratePairingKey.setBounds(296, 347, 220, 50);
		btnGeneratePairingKey.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				Random rand = new Random();
				BigInteger exp = BigInteger.valueOf((rand.nextInt(89999)+10000));
				
				ArrayList<String> config = windowsApp.getFileIO().getConfigFile();
				for(int i = 0; i < config.size(); i++){
					String s = config.get(i);
					String[] value = s.split(":");
					if(value[0].equals("rsaexp")){
						String t = value[0] + ":" + exp;
						config.set(i, t);
						break;
					}
				}
				windowsApp.getFileIO().updateConfigFile(config);
				windowsApp.getCSEP().getConnectionServiceImpPort().updateDevicePair(windowsApp.getDevice().getUUID(), String.valueOf(exp));
				
				RSAPublicKeySpec publicKey = windowsApp.generatePublicKey(exp);
				RSAPrivateKeySpec privateKey = windowsApp.generatePrivateKey(exp);
				lblPairKey.setText(publicKey.getPublicExponent().toString());
			}
		});
		pnlPairing.add(btnGeneratePairingKey);		
		
		JButton btnBack = new JButton("Back");
		btnBack.setBounds(691, 562, 97, 25);
		btnBack.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				windowsApp.getFrame().setContentPane(new Overview(windowsApp).getPanel());
			}
		});
		pnlPairing.add(btnBack);
	}
	
	private String getRSAExp(){
		String exp = "";
		ArrayList<String> config = windowsApp.getFileIO().getConfigFile();
		for(int i = 0; i < config.size(); i++){
			String s = config.get(i);
			String[] value = s.split(":");
			if(value[0].equals("rsaexp")){
				exp = value[1];
				break;
			}
		}
		
		
		return exp;
	}
	
	public JPanel getPanel(){
		return pnlPairing;
	}
}
